package de.breitbeil.samsyncro.lib.domain.parser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import de.breitbeil.samsyncro.lib.domain.fs.SFile;

public class SFileParser {
    
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

    public static ArrayList<SFile> parseList(String s) {
        ArrayList<SFile> files = new ArrayList<SFile>();
        String[] splitted = s.split("\\\\r\\\\n");
        for(String file: splitted) {
            files.add( parse(file) );
        }
        return files;
    }
    
    public static SFile parse(String parse) {
        try {
            String[] splitted = parse.split(",");
            String name = splitted[0];
            int size = Integer.parseInt(splitted[1]);
            String unknown = splitted[2];
            Date created = sdf.parse(splitted[3]);
            
            SFile sFile = new SFile();
            sFile.setName(name);
            sFile.setSize(size);
            sFile.setCreated(created);
            sFile.setCreated(created);
            
            return sFile;
        }
        catch(Exception e) {
            throw new IllegalArgumentException("Cant parse file String: "+parse, e);
        }
    }
    
}
